package hadoop.ch05.v17124080106;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class SalaryTotalMapper extends Mapper< LongWritable, Text, IntWritable,  Employee> {

    @Override
    protected void map(LongWritable key1, Text value1,
                       Context context)
            throws IOException, InterruptedException {
        //数据：7499,ALLEN,SALESMAN,7698,1981/2/20,1600,300,30
        String data = value1.toString();
        String[] words = data.split(",");

        //创建员工对象
        Employee e = new Employee();
        //设置员工属性
        e.setEmpno(Integer.parseInt(words[0]));

        e.setEname(words[1]);

        e.setJob(words[2]);

        try {
            e.setMgr(Integer.parseInt(words[3]));//可能为空,加try...catch包围
        } catch (Exception ex) {
            e.setMgr(-1);
        }

        e.setHiredate(words[4]);

        e.setSal(Integer.parseInt(words[5]));

        try {
            e.setComm(Integer.parseInt(words[6]));//可能为空
        } catch (Exception ex) {
            e.setComm(0);
        }

        e.setDeptno(Integer.parseInt(words[7]));
        context.write(new IntWritable(e.getDeptno()),e);
    }

}
